package com.sinoglobal.ningxia.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.sinoglobal.dumping.activity.Dumpling_MainActivity;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.EnentItemActivity;
import com.sinoglobal.ningxia.activity.amusement.SettleDownDetailsActivity;
import com.sinoglobal.ningxia.activity.mico.ProgramInfoActivity;
import com.sinoglobal.ningxia.activity.news.NewsDetailActivity;
import com.sinoglobal.ningxia.beans.HomeBannerBean;
import com.sinoglobal.ningxia.beans.MicProgramBean;
import com.sinoglobal.ningxia.beans.NewsBean;
import com.sinoglobal.ningxia.fragment.SettleDownInfoFragment;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.qr.QrUrlResultActivity;
import com.sinoglobal.ningxia.utils.FlyUtil;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.TextUtil;
import com.sinoglobal.sinostore.activity.FirstActivity;
import com.sinoglobal.sinostore.activity.GoodsDetailActivity;
import com.sinoglobal.sinostore.activity.GoodsListActivity;
import com.sinoglobal.sinostore.activity.SecondKillActivity;
import com.sinoglobal.sinostore.system.Constants;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;

public class HomeBannerAdapter extends PagerAdapter {
    private ArrayList<HomeBannerBean> tvList; // 存放广告图片的列表
    FinalBitmap fb;
    Bitmap defaultPic;
    Activity activity;
    int size;
    private ImageView[] imageViews;// 存放banner图

    public HomeBannerAdapter(Activity activity, ArrayList<HomeBannerBean> list) {
        super();
        this.activity = activity;
        tvList = list;
        defaultPic = BitmapFactory.decodeResource(activity.getResources(), R.drawable.default21);
        fb = FinalBitmap.create(activity);
        size = tvList.size();
        createImg(size, activity);
    }

    private void createImg(int num, Activity activity) {
        imageViews = new ImageView[num];
        for (int i = 0; i < num; i++) {
            imageViews[i] = new ImageView(activity);
            imageViews[i].setScaleType(ScaleType.CENTER_CROP);
        }

    }

    // 代表的是当前传进来的对象，是不是要在我当前页面显示的
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // Auto-generated method stub
        return arg0 == arg1;// 如果当前显示的View跟你传进来的是同一个View,说明就是要显示的 view
    }

    @Override
    // 图片的数量
    public int getCount() {
        return tvList.size();
        // return Integer.MAX_VALUE;
    }

    /**
     * 移除当前一张图片
     */
    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {
        view.removeView(imageViews[position % size]);
    }

    /**
     * 添加一张图片
     */
    @Override
    public Object instantiateItem(ViewGroup view, final int position) {

        int pos = position % size;

        if (imageViews[pos].getParent() == null) {
            view.addView(imageViews[pos]);
        } else {
            ((ViewGroup) imageViews[pos].getParent()).removeView(imageViews[pos]);
            view.addView(imageViews[pos]);
        }
        /* view.addView(imageViews[pos], 0); */

        String imgUrl = tvList.get(pos).getBanner();
        if (!imgUrl.equals("")) {
            imgUrl = ConnectionUtil.localUrl + imgUrl;
            fb.display(imageViews[pos], imgUrl, defaultPic, defaultPic);
        }

        imageViews[pos].setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if ("11".equals(tvList.get(position % size).getType())) {
                    Intent intent = new Intent(activity, Dumpling_MainActivity.class);
                    activity.startActivity(intent);
                } else {
                    Log.e("webUrl", tvList.get(position % size).getLink() + "===" + tvList.get(position % size).getUrl());
                    goIntent(tvList.get(position % size).getLink(), tvList.get(position % size).getUrl(), position, tvList.get(position % size).getIntro());
                }

            }
        });
        return imageViews[pos];
		/*
		 * if (tvList.get(position % size).getIv() == null) return null; return
		 * tvList.get(position % size).getIv();
		 */
    }

    /**
     * @throws Exception
     * @author qiwx
     * @createdate 2014-7-14 下午4:42:15
     * @Description:根据banner图的link地址跳转到相应的界面
     * @type #activity#type#id type 1=新闻|2=栏目|3=活动|4=安居|5=商铺, id 代表目标id
     * 7=商城首页|8=商城分类|9=商品详情|10=商城活动
     */
    private void goIntent(String link, String url, int position, String intro) {
        HomeBannerBean store_bean = tvList.get(position);
        if (store_bean == null)
            return;
        String[] str = link.split("#");
        if (str == null || str.length == 0)
            return;
        int len = str.length;
        if (len < 3)
            return;
        if (str[0].equals("activity")) {
            byte type;
            String id = MContants.EMPTY_STRING;

            try {
                type = Byte.parseByte(str[1]);
                id = str[2];
            } catch (Exception e) {
                e.printStackTrace();
                type = 0;
            }
            switch (type) {
                case 0:
                    break;
                case 1: // 新闻
                    NewsBean bean = new NewsBean();
                    bean.setId(id);
                    Intent go = new Intent(activity, NewsDetailActivity.class);
                    go.putExtra(NewsBean.class.getName(), bean);
                    activity.startActivity(go);
                    break;
                case 2:// 栏目
                    MicProgramBean programBean = new MicProgramBean();
                    programBean.setLanmuid(str[2]);
                    Intent goProgram = new Intent(activity, ProgramInfoActivity.class);
                    goProgram.putExtra(MicProgramBean.class.getName(), programBean);
                    activity.startActivity(goProgram);
                    break;
                case 3:// 活动
                    Intent intent = new Intent(activity, EnentItemActivity.class);
                    // 将点击处的ID传递给下一个页面
                    intent.putExtra("id", id);
                    activity.startActivity(intent);
                    break;
                case 4:// 安居,去掉
                    Intent AnJu = new Intent(activity, SettleDownDetailsActivity.class);
                    // 将点击处的ID传递给下一个页面
                    AnJu.putExtra(SettleDownInfoFragment.HOUSEID, str[2]);
                    activity.startActivity(AnJu);
                    break;
                case 5:// 商铺去掉

                    break;
                case 6:// 广告
                    String ad_url = url;
                    if (TextUtil.stringIsNull(ad_url))
                        return;
                    // TODO 6链接地址中可能会有userId字段
                    Intent ad_intent = new Intent();
                    ad_intent.setClass(activity, QrUrlResultActivity.class);
                    ad_intent.putExtra("captureResult", ad_url);
                    ad_intent.putExtra("intro", intro);
                    ad_intent.putExtra("isShare", true);
                    activity.startActivity(ad_intent);
                    break;
                // 商城首页
                case 7:
                    Intent storeIntent = new Intent(activity, FirstActivity.class);
                    FlyUtil.intentForward(activity, storeIntent);
                    break;
                // 8=商城分类
                case 8:
                    Intent storeList = new Intent(activity, GoodsListActivity.class);
                    storeList.putExtra("typeId", store_bean.getTarget_id());
                    storeList.putExtra("fromHomePage", false);
                    if (null != store_bean.getType_name())
                        storeList.putExtra("typeName", store_bean.getType_name());
                    Constants.SORT_POSITION = 0;
                    FlyUtil.intentForward(activity, storeList);
                    break;
                // 9=商品详情
                case 9:
                    Intent shopDetail = new Intent(activity, GoodsDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("goodsId", store_bean.getTarget_id());
                    shopDetail.putExtras(bundle);
                    FlyUtil.intentForward(activity, shopDetail);
                    break;
                // 10=商城活动
                case 10:
                    Intent activityList = new Intent(activity, SecondKillActivity.class);
                    activityList.putExtra("id", store_bean.getType_id());
                    activityList.putExtra("activityId", store_bean.getTarget_id());
                    FlyUtil.intentForward(activity, activityList);
                    break;
            }
        }

    }

}